<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view v-if="!addressArr" class="address-btn u-flex u-row-center">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          @click="handleChooseAddress"
          >获取收货地址</u-button
        >
      </view>

      <view v-else class="address-detail">
        <view class="address-detail">{{detail}}</view>
        <view class="address-user">{{addressArr.userName}}--{{addressArr.telNumber}}</view>
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view class="cart-item" v-for="item in checkedCartList" :key="item.goods_id">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image width="191rpx" height="191rpx" :src="item.goods_small_logo"></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{item.goods_name}}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{item.goods_price}}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X {{item.num}}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price"> 合计: <text>￥{{totalPrice}}</text> </view>
      <view class="all-count">
        <u-button type="error" shape="circle"  @click="handlePay">去支付({{totalCount}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import {mapGetters, mapActions, mapState} from 'vuex'
export default {
    data() {
        return {
            addressArr: null
        }
    },
    computed: {
        ...mapGetters('cart',['checkedCartList','totalCount','totalPrice']),
        ...mapState('user',['token']),
        // 拼接地址路径
        //cityName: "广州市"
        // countyName: "海珠区"
        // detailInfo: "新港中路397号"
        // errMsg: "chooseAddress:ok"
        // nationalCode: "510000"
        // postalCode: "510000"
        // provinceName: "广东省"
        // telNumber: "020-81167888"
        // userName: "张三"
        detail() {
            if (!this.addressArr) return "";
                const {provinceName,cityName,countyName,detailInfo} = this.addressArr
                return provinceName+cityName+countyName+detailInfo
        }
    },
    methods: {
        ...mapActions('user',['getToken']),
        
        // 点击收货地址，获取到微信本地上保存你的数据
        async handleChooseAddress() {
            const [err,addressArr] = await uni.chooseAddress()
            if(err) {
                console.log(err);
                return
            }
            this.addressArr = addressArr
        },
        // 去支付页面
        async handlePay() {
            // 0 判断用户还没有选择收货地址
            if (!this.addressArr) {
                uni.showToast({
                title: "您还没有选择收货地址",
                icon: "none",
                });
                return;
            }
            // 获取用户性息和微信临时凭据换取token
            // 1 获取用户信息
            // const [err,resault] = await uni.getUserProfile({
            //     desc: '用于获取数据，来完成支付功能'
            // })
            // const {encryptedData,rawData,iv,signature} = resault
            // console.log(102,err,resault);
            // 2 执行微信的登录 login 获取临时登录凭据
            // const [err2, res2] = await uni.login();
            // const { code } = res2;
            // console.log(res2);

            // 3 换取token
            // const {token} = await this.$u.http.post('/users/wxlogin',{encryptedData,rawData,iv,signature,code})
            // console.log(112,token);
            await this.getToken()
            // console.log(122,this.token);
            // return
            // 4 创建订单，获取订单编号
            const orderParams = {
                order_price: this.totalPrice,
                consignee_addr: this.detail,
                goods: this.checkedCartList.map((goods) => ({
                goods_id: goods.goods_id,
                goods_number: goods.num,
                goods_price: goods.goods_price,
                })),
            };
            const orderNumber = await this.$u.http.post('/my/orders/create',orderParams,{Authorization: this.token})
            console.log(124,orderNumber);
            // 5 获取支付参数
            const payData = await this.$u.http.post('/my/orders/req_unifiedorder',orderNumber,{Authorization: this.token})
            console.log(127,payData);
            // 6 传入支付参数，调用微信支付
            uni.requestPayment(payData.pay);
        }
    }
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $theme-color;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>